import java.util.Arrays;

public class Test {
    public static void main8(String[] args) {

    }


















    public static int avg(int[] a){
        int sum = 0;
        for (int i = 0; i < a.length; i++) {
            for (int x :
                    a) {
                sum += x;
            }
        }
        return sum/a.length;
    }
    public static void main9(String[] args) {
        //方法求数组元素平均值
        int[] a ={12,342,56,67,78,333};
        System.out.println(avg(a));
    }
















    public static void main2(String[] args) {
        //求数组中元素的平均值
        int[] a = {3,4543,5654,4676,23,2};
        int sum = 0;
        for (int x : a) {
            sum += x;
        }
        System.out.println(sum/a.length);
    }

















    public static void main(String[] args) {
        //数组拷贝
        int[] a = {1,2,3,4,234,45353};
//        int[] b = new int[a.length];//注意a.length的使用
//        for (int i = 0; i < a.length; i++) {
//            b[i] = a[i];
//        }
//        System.out.println(Arrays.toString(b));
//        Arrays.copyOf(a,a.length);     //返回类型为数组，可以拷贝数组的一段
        System.out.println();
//        System.out.println(Arrays.toString(Arrays.copyOfRange(a,0,3)));//可以拷贝一个范围，左闭右开
        System.out.println(Arrays.toString(Arrays.copyOfRange(a,0,3)));
    }



















    public static void main6(String[] args) {
        int[] array1 = {1,2,3,4,5};
        int[] array3 = {1,2,3,4,5};
        int[] array4 = new int[10];
        System.out.println(Arrays.equals(array3, array1));//equals判断数组是否一样
        Arrays.fill(array4,0,2,2);//填充
        System.out.println(Arrays.toString(array4));













    }
    public static int func1(int[] array){
        Arrays.sort(array);
        return array[array.length/2];
    }
    public static void main5(String[] args) {
        int[] a ={1,2,2,1,1,1,2,2};
        System.out.println(func1(a));


    }









    //给定一个数组，求出其中不同的元素
    public static int func(int[] array){
        int ret = 0;
        for (int i = 0; i < array.length; i++) {
            ret ^= array[i];
        }
        return ret;
    }
    public static void main4(String[] args) {
        int[] array = {2,2,4,5,5,6,6,8,8};
        System.out.println(func(array));

    }












    //给定一个数字，数组，求在数组中两个元素和为给定数字的下标
    public static int[] findNum(int[] array,int target){
        int[] ret = {-1,-1};
        for (int i = 0; i < array.length; i++) {//遍历
            for (int j = i+1; j < array.length ; j++) {//遍历
                if (array[i] + array[j] == target){
                    ret[0] = i;
                    ret[1] = j;
                }
            }
        }
        return ret;
    }
    public static void main3(String[] args) {
        int[] array = {2,4,6,3};
        int[] ret = findNum(array,10);
        System.out.println(Arrays.toString(ret));
    }









    //二分查找
    public static int binarySearch(int[] array,int key){
        int i = 0;
        int j = array.length-1;//
        while (i<=j){
            int mid = (i+j)/2;
            if (array[mid]>key){
                j = mid-1;//关键字在左边，所以要把中间值往左边移动
            }else if (array[mid] == key){
                return mid;
            }else {
                i = mid+1;//关键字在右边，所以要把中间值往右边移动
                //如果不加1，i的值会一直不变
            }
        }
        return -1;
    }
    public static void main1(String[] args) {
        int[] array = {1,2,3,4,5,6,7};
//        System.out.println(binarySearch(array,2));
//        System.out.println(binarySearch(array,5));
//        System.out.println(binarySearch(array,3));
        System.out.println(binarySearch(array,8));
        System.out.println(Arrays.binarySearch(array,110));
    }
}
